package com.sbs.gaoxinqutoilet.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.ListPopupWindow;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseBackgroundFormat;
import com.bin.david.form.data.format.sequence.NumberSequenceFormat;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.style.LineStyle;
import com.bin.david.form.data.table.TableData;
import com.bin.david.form.utils.DensityUtils;
import com.google.gson.reflect.TypeToken;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.base.BaseBackFragment;
import com.sbs.gaoxinqutoilet.entity.CountyEntity;
import com.sbs.gaoxinqutoilet.entity.HomeMenuEntity;
import com.sbs.gaoxinqutoilet.entity.TongjiEntity;
import com.sbs.gaoxinqutoilet.entity.UserEntity;
import com.sbs.gaoxinqutoilet.eventbus.EventCenter;
import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.http.Request;
import com.sbs.gaoxinqutoilet.interfaces.Constants;
import com.sbs.gaoxinqutoilet.interfaces.ContyPopuListener;
import com.sbs.gaoxinqutoilet.interfaces.EventConstant;
import com.sbs.gaoxinqutoilet.interfaces.PopuClickListener;
import com.sbs.gaoxinqutoilet.myapp.MyApplication;
import com.sbs.gaoxinqutoilet.utils.GsonTools;
import com.sbs.gaoxinqutoilet.utils.HttpUtils;
import com.sbs.gaoxinqutoilet.utils.JsonUtil;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.utils.SPUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2018/6/25.
 * Time: 2018/6/25  16:56
 */

public class ToiletStatisTicalFragmentD extends BaseBackFragment {

    //传过来的改造档案数据  存储在Bundle中
    private static final String STATISTICAL_FILE = "statistical_file";
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.tongji_trefresh)
    SmartRefreshLayout tongjiTrefresh;
    @BindView(R.id.tongji_table)
    SmartTable<TongjiEntity.DataBean> tongjiTable;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    Unbinder unbinder;
    @BindView(R.id.toolbar_iv_add)
    ImageView toolbarIvAdd;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.file_address_conty)
    TextView fileAddressConty;
    @BindView(R.id.file_address_town)
    TextView fileAddressTown;
    @BindView(R.id.file_address_village)
    TextView fileAddressVillage;
    @BindView(R.id.file_search_all)
    AutoLinearLayout fileSearchAll;
    @BindView(R.id.all_sliding_licate)
    AutoLinearLayout allSlidingLicate;
    @BindView(R.id.toilet_reset)
    Button toiletReset;
    @BindView(R.id.toilet_done)
    Button toiletDone;
    @BindView(R.id.drawlayout_tongji)
    DrawerLayout drawlayoutTongji;
    @BindView(R.id.tongji_tv_nianfen)
    TextView tongjiTvNianfen;
    @BindView(R.id.tongji_nianfen)
    AutoLinearLayout tongjiNianfen;
    private HomeMenuEntity homemenu_buildfile;

    private int pageSize = 20;
    private int index;
    private List<TongjiEntity.DataBean> dataGets = new ArrayList<>();
    private TableData<TongjiEntity.DataBean> tableData;
    private Column<String> column1;
    private Column<Integer> column2;
    private Column<String> column3;
    //private Column<String> column4;
    private Column<String> column5;
    private Column<String> column6;
    private Column<String> columnAll;
    private String token_data;
    private ContyRequest cavRequest;
    private TwonRequest twonRequest;
    private VillageRequest villageRequest;
    private ListPopupWindow filecontyPW;

    private List<String> namelist = new ArrayList<>();
    private ListPopupWindow filetwonPW;
    private ListPopupWindow filevillagePW;
    private Map<String, String> conditionmap = new HashMap<>();
    private List<String> tongji_nianfen = new ArrayList<>();
    private UserEntity userEntity;
    private boolean isXian;
    private String tongjiUrl;


    public static ToiletStatisTicalFragmentD newInstance(HomeMenuEntity homeMenuEntity) {
        Bundle args = new Bundle();
        args.putSerializable(STATISTICAL_FILE, homeMenuEntity);
        ToiletStatisTicalFragmentD fragment = new ToiletStatisTicalFragmentD();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_statistical, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(inflater);
        initListener();

        return attachToSwipeBack(view);
    }


    private void initView(LayoutInflater inflater) {
        EventBus.getDefault().register(this);
        toolbarTitle.setText("改厕进度统计");
        //返回按钮
        toolbarBack.setVisibility(View.VISIBLE);
        toolbarIvMenu.setVisibility(View.VISIBLE);
        toolbarIvMenu.setImageResource(R.drawable.searchchoose);
        //设置回键
        initToolbarBack(toolbar);

        token_data = SPUtil.getString(Constants.KEY_TOKEN, "");


        //获取传递过来的值
        Bundle arguments = getArguments();
        if (arguments != null) {
            homemenu_buildfile = (HomeMenuEntity) arguments.getSerializable(STATISTICAL_FILE);
            if (homemenu_buildfile != null) {
                LogUtils.i("统计页面", "initView: " + homemenu_buildfile.getFunc_name() + "~~~" + homemenu_buildfile.getFunc_id());
                //showShortToast(homemenu_buildfile.getFunc_name()+"~~~"+homemenu_buildfile.getFunc_id());
            }

        }




        tongji_nianfen.add("2018");
        tongji_nianfen.add("2019");
        tongji_nianfen.add("2020");
        tongji_nianfen.add("2021");

        //初始化index
        index = 0;

        //添加刷新头和尾
        SinaRefreshView sinaHeader = new SinaRefreshView(_mActivity);
        sinaHeader.setArrowResource(R.drawable.arrow);
        sinaHeader.setTextColor(0xff745D5C);


        tongjiTrefresh.setRefreshHeader(new ClassicsHeader(_mActivity));

        tongjiTrefresh.setRefreshFooter(new ClassicsFooter(_mActivity));


        //去掉顶部ABC
        FontStyle.setDefaultTextSize(DensityUtils.sp2px(_mActivity, 15)); //设置全局字体大小
        column1 = new Column<>("辖区单位", "area_name");
        column1.setAutoCount(true);
        column2 = new Column<>("户数", "zonghushu");
        column2.setAutoCount(true);
        column3 = new Column<>("任务数量", "zongrenwu");
        column3.setAutoCount(true);
//        column4 = new Column<>("2018年", "yigaizao_2018");
//        column4.setAutoCount(true);
        column5 = new Column<>("2020年之前", "yigaizao_2019");
        column5.setAutoCount(true);
        column6 = new Column<>("2020年", "yigaizao_2020");
        column6.setAutoCount(true);
        //columnAll = new Column<>("已改造数量", column4, column5, column6);
        columnAll = new Column<>("已改造数量", column5, column6);


        tongjiTable.getConfig().setShowYSequence(true);
        tongjiTable.getConfig().setShowXSequence(false);


        tongjiTable.getConfig().setColumnTitleStyle(new FontStyle().setTextColor(R.color.black));
        tongjiTable.getConfig().setContentGridStyle(new LineStyle().setColor(R.color.black));
        tongjiTable.getConfig().setColumnTitleGridStyle(new LineStyle().setColor(R.color.black));

        tongjiTable.getConfig().setColumnTitleBackground(new BaseBackgroundFormat(getResources().getColor(R.color.windows_bg)));
        tongjiTable.getConfig().setCountBackground(new BaseBackgroundFormat(getResources().getColor(R.color.windows_bg)));


    }


    private boolean isFrist = true;

    private void initListener() {

        tongjiTrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                index = 0;
                // showLoadingDialog();
                new Handler() {
                }.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadMoreData(index, pageSize, isFrist, conditionmap);
                    }
                }, 500);

                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        tongjiTrefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                index++;

                //showLoadingDialog();
                new Handler() {
                }.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadMoreData(index * pageSize, pageSize, true, conditionmap);
                    }
                }, 500);


            }
        });

        toolbarIvMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (drawlayoutTongji.isDrawerOpen(GravityCompat.END)) {
                    drawlayoutTongji.closeDrawer(GravityCompat.END);
                } else {
                    drawlayoutTongji.openDrawer(GravityCompat.END);
                }


            }
        });


    }


    /**
     * eventbus 页面跳转,事件接受
     *
     * @param center
     */
    @Subscribe
    public void onEventReceive(EventCenter center) {

        if (center.getEventCode() == EventConstant.UPLOAD_BUY_MODIFY) {
            //提交成功
            LogUtils.i("显示", "刷新");
            new Handler() {
            }.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // requestRefresh();
                }
            }, 500);
        }
    }

    /**
     * 页面加载动画结束就调用  loadMoreData(index, pageSize);
     * 所以  将两个方法紧挨着
     *
     * @param savedInstanceState
     */
    @Override
    public void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        //添加家在逻辑
        requestRefresh();

        //获取县乡村数据
        getLevelData();


    }

    private void requestRefresh() {

        //设置出现刷新状态的头部
        if (tongjiTrefresh != null) {
            tongjiTrefresh.finishRefresh();
        }
        index = 0;

        loadMoreData(index, pageSize, isFrist, conditionmap);
    }


    private void loadMoreData(final int index, final int pageSize, final boolean isFoot, Map<String, String> conditionmap) {

        if(isXian){

            tongjiUrl = HttpApis.tongjiXianDate;
        }else{
            tongjiUrl = HttpApis.tongjiDate;
        }



        HashMap<String, String> params = new HashMap<>();
        params.put("iud", homemenu_buildfile.getIud());
        params.put("start", String.valueOf(index));
        params.put("pagesize", String.valueOf(pageSize));
        params.put("token", token_data);
        if (conditionmap != null && conditionmap.size() > 0) {
            params.putAll(conditionmap);
        }

        HttpUtils.requestGet(_mActivity, tongjiUrl, params, new Request() {
            @Override
            public void success(String jsonStr) {
                super.success(jsonStr);

                LogUtils.i("buildfile_success", jsonStr);

                TongjiEntity tongjiEntity = GsonTools.changeGsonToBean(jsonStr, TongjiEntity.class);
                List<TongjiEntity.DataBean> data = tongjiEntity.getData();

//                if(data!=null&&data.size()>0){
//                    TongjiEntity.DataBean dataBean = data.get(0);
//                    String area_name_short = dataBean.getArea_name_short();
//                    for (int i=0;i<data.size();i++) {
//                        if(i==0){
//                            continue;
//                        }
//                        data.get(i).setArea_name_short("临汾市翼城县"+area_name_short+data.get(i).getArea_name_short());
//                    }
//                }


                String recordsTotal = tongjiEntity.getRecordsTotal();
                try {
                    if (index == 0) {
                        tongjiTrefresh.finishRefresh();
//                        tongjiTable.setData(null);
//                        tongjiTable.setData(data);

                        if (dataGets != null && dataGets.size() > 0) {
                            dataGets.clear();
                        }
                        dataGets.addAll(data);
                        tableData = new TableData<>("统计表", dataGets, column1, column2,
                                column3, columnAll);
                        //tableData.setShowCount(true); //显示总和数据
                        tableData.setYSequenceFormat(new NumberSequenceFormat() {
                            @Override
                            public String format(Integer integer) {
                                return (integer - 1) + "";
                            }
                        });
                        tongjiTable.setTableData(tableData);

                    } else {

                        int size = tongjiTable.getTableData().getT().size();
                        size = size + data.size();
                        int totalPageSize = Integer.parseInt(recordsTotal);
                        if (size <= totalPageSize) {
                            //tongjiTable.addData(data,isFoot);
                            if (dataGets != null && dataGets.size() > 0) {
                                dataGets.addAll(data);
                                tableData = new TableData<>("统计表", dataGets, column1, column2,
                                        column3, columnAll);
                                //tableData.setShowCount(true);//显示总和数据
                                tableData.setYSequenceFormat(new NumberSequenceFormat() {
                                    @Override
                                    public String format(Integer integer) {
                                        return (integer - 1) + "";
                                    }
                                });
                                tongjiTable.setTableData(tableData);
                            }

                        }
                        LogUtils.i("hashiqisize", "lineSize: " + size);
                        tongjiTrefresh.finishLoadMore();

                    }
                    cancelLoadingDialog();
                } catch (Exception e) {

                }
            }

            @Override
            public void error(String error) {
                super.error(error);
                LogUtils.i("buildfile_error", error);
                if (!isFoot) {
                    tongjiTrefresh.finishRefresh(false);
                } else {
                    tongjiTrefresh.finishLoadMore(false);
                }
                cancelLoadingDialog();
            }

        });

        isXian=false;

    }


    @OnClick({R.id.file_address_conty, R.id.file_address_town,
            R.id.file_address_village, R.id.toilet_reset,
            R.id.toilet_done, R.id.tongji_tv_nianfen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.file_address_conty:

                filecontyPW = new ListPopupWindow(_mActivity);
                setPopuDataList(view, filecontyPW, contylist);
                setPopuClickListenerList(new ContyPopuListener() {
                    @Override
                    public void show(CountyEntity Centity, String text) {
                        fileAddressConty.setText(Centity.getText());
                        //showLoadingDialog("正在加载乡镇数据");
                        SPUtil.putString(Constants.TongJI_CONTY_LEVEL, "xian");
                        SPUtil.putString(Constants.TongJI_CONTY_ID, Centity.getValue());

                        getTownList(Centity.getValue());
                    }
                });
                break;
            case R.id.file_address_town:
                String conty_address = fileAddressConty.getText().toString().trim();
                if (TextUtils.isEmpty(conty_address) || "县".equals(conty_address)) {
                    showShortToast("请先选择县级单位");
                    return;
                } else {

                    try {
                        if (twonlist != null && twonlist.size() > 0) {
                            filetwonPW = new ListPopupWindow(_mActivity);
                            setPopuDataList(view, filetwonPW, twonlist);
                            setPopuClickListenerList(new ContyPopuListener() {
                                @Override
                                public void show(CountyEntity Centity, String text) {

                                    fileAddressTown.setText(Centity.getText());
                                    SPUtil.putString(Constants.TongJI_CONTY_LEVEL, "xiang");
                                    SPUtil.putString(Constants.TongJI_CONTY_ID, Centity.getValue());
                                    //showLoadingDialog("正在加载村级数据");
                                    getVillageList(Centity.getValue());

                                }
                            });
                        }
                    } catch (Exception e) {
                        Log.i("list为null", "乡级列表onViewClicked: " + e.toString());
                    }
                }
                break;
            case R.id.file_address_village:

                String town_address = fileAddressTown.getText().toString().trim();
                if (TextUtils.isEmpty(town_address) || "乡".equals(town_address)) {
                    showShortToast("请先选择乡级单位");
                    return;
                } else {
                    try {
                        if (villagelist != null && villagelist.size() > 0) {
                            filevillagePW = new ListPopupWindow(_mActivity);
                            setPopuDataList(view, filevillagePW, villagelist);
                            setPopuClickListenerList(new ContyPopuListener() {
                                @Override
                                public void show(CountyEntity Centity, String text) {
                                    fileAddressVillage.setText(Centity.getText());
                                    SPUtil.putString(Constants.TongJI_CONTY_LEVEL, "cun");
                                    SPUtil.putString(Constants.TongJI_CONTY_ID, Centity.getValue());
                                }
                            });
                        }
                    } catch (Exception e) {
                        Log.i("list为null", "村级列表onViewClicked: " + e.toString());
                    }
                }

                break;

            case R.id.tongji_tv_nianfen:

                ListPopupWindow acceptResultPopupWindow = new ListPopupWindow(_mActivity);
                setPopuData(view, acceptResultPopupWindow, tongji_nianfen);
                setPopuClickListener(new PopuClickListener() {
                    @Override
                    public void show(String mess) {
                        tongjiTvNianfen.setText(mess);
                    }
                });

                break;
            case R.id.toilet_reset:
                if (conditionmap != null) {
                    conditionmap.clear();
                }

                drawlayoutTongji.closeDrawer(GravityCompat.END);

                fileAddressConty.setText("(区)县");
                fileAddressTown.setText("乡");
                fileAddressVillage.setText("村");
                break;
            case R.id.toilet_done:

                drawlayoutTongji.closeDrawer(GravityCompat.END);
                index=0;

                String addressConty = fileAddressConty.getText().toString();
                String addressTown = fileAddressTown.getText().toString();
                String addressvillage = fileAddressVillage.getText().toString();

                if (!"(区)县".equals(addressConty) | !"乡".equals(addressTown) | !"村".equals(addressvillage)) {
                    String conty_level = SPUtil.getString(Constants.TongJI_CONTY_LEVEL, "");
                    if ("xian".equals(conty_level)) {
                        if (conditionmap.containsKey("a.xiang_area_id")) {
                            conditionmap.remove("a.xiang_area_id");
                        }
                        if (conditionmap.containsKey("a.area_id")) {
                            conditionmap.remove("a.area_id");
                        }
                        conditionmap.put("a.xian_area_id", SPUtil.getString(Constants.TongJI_CONTY_ID, ""));
                    } else if ("xiang".equals(conty_level)) {
                        if (conditionmap.containsKey("a.xian_area_id")) {
                            conditionmap.remove("a.xian_area_id");
                        }
                        if (conditionmap.containsKey("a.area_id")) {
                            conditionmap.remove("a.area_id");
                        }
                        conditionmap.put("a.xiang_area_id", SPUtil.getString(Constants.TongJI_CONTY_ID, ""));
                    } else if ("cun".equals(conty_level)) {
                        if (conditionmap.containsKey("a.xian_area_id")) {
                            conditionmap.remove("a.xian_area_id");
                        }
                        if (conditionmap.containsKey("a.xiang_area_id")) {
                            conditionmap.remove("a.xiang_area_id");
                        }
                        conditionmap.put("a.area_id", SPUtil.getString(Constants.TongJI_CONTY_ID, ""));
                    }
                }

//                String nianfen = tongjiTvNianfen.getText().toString().trim();
//                if(!TextUtils.isEmpty(nianfen)&&!"下拉选择".equals(nianfen)){
//                    conditionmap.put("nianfen",nianfen);
//                }

                //301 市  302 县  303 乡 304*
                userEntity = MyApplication.getUserEntity();
                if(userEntity!=null){
                    if("302".equals(userEntity.getOpt_level())){
                        isXian = true;
                    }else{
                       isXian=false;
                    }

                    loadMoreData(index, pageSize, isFrist, conditionmap);
                }




                if (conditionmap != null) {
                    conditionmap.clear();
                }

                fileAddressConty.setText("(区)县");
                fileAddressTown.setText("乡");
                fileAddressVillage.setText("村");


                break;
        }
    }


    private void setPopuDataList(View view, final ListPopupWindow mPopupWindow, final List<CountyEntity> datalists) {

        //清空集合,添加数据
        namelist.clear();
        for (CountyEntity ce : datalists) {
            namelist.add(ce.getText());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_list_item_1, namelist);
        mPopupWindow.setAdapter(adapter);

        //弹出的位置  弹出锚点
        mPopupWindow.setAnchorView(view);
        //设置宽高
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setModal(true);//设置模态框，设置为true响应物理键
        //设置条目点击事件
        mPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //使用回调,显示数据
                if (popuClickListenerList != null) {
                    popuClickListenerList.show(datalists.get(position), datalists.get(position).getText());
                }
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.show();
    }

    public ContyPopuListener popuClickListenerList;

    public void setPopuClickListenerList(ContyPopuListener popuClickListenerList) {
        this.popuClickListenerList = popuClickListenerList;
    }


    private void setPopuData(View view, final ListPopupWindow mPopupWindow, final List<String> datalist) {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_list_item_1, datalist);
        mPopupWindow.setAdapter(adapter);

        //弹出的位置  弹出锚点
        mPopupWindow.setAnchorView(view);
        //设置宽高
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

        mPopupWindow.setModal(true);//设置模态框，设置为true响应物理键
        //设置条目点击事件
        mPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //使用回调,显示数据
                if (popuClickListener != null) {
                    popuClickListener.show("" + datalist.get(position));
                }
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.show();
    }


    public PopuClickListener popuClickListener;

    public void setPopuClickListener(PopuClickListener popuClickListener) {
        this.popuClickListener = popuClickListener;
    }


    /**
     * 获取县乡村数据
     */
    private void getLevelData() {
        //获取县,乡,村级列表数据
        //获取个人信息
        UserEntity userEntity = SPUtil.getBeanValue(Constants.KEY_USER, UserEntity.class);
        if (userEntity != null) {
            //获取县级列表
            getContyList(userEntity);
        }

    }


    //县级列表
    private List<CountyEntity> contylist = null;
    //乡级列表
    private List<CountyEntity> twonlist = null;
    //村级列表
    private List<CountyEntity> villagelist = null;

    private void getContyList(UserEntity userEntity) {
        //需要拼接
        String addnew_url = HttpApis.addnew_info + userEntity.getArea_id() + "/" + "3";
        if (cavRequest == null) {
            cavRequest = new ContyRequest();
        }
        LogUtils.i("乡级列表", "url: " + addnew_url);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token_data);
        HttpUtils.requestGet(_mActivity, addnew_url, map, cavRequest);
    }

    private void getTownList(String conty_id) {
        String addnew_url = HttpApis.addnew_info + conty_id + "/" + "4";
        if (twonRequest == null) {
            twonRequest = new TwonRequest();
        }
        LogUtils.i("县级列表", "url: " + addnew_url);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token_data);
        HttpUtils.requestGet(_mActivity, addnew_url, map, twonRequest);
    }

    private void getVillageList(String teon_id) {
        String addnew_url = HttpApis.addnew_info + teon_id + "/" + "5";
        if (villageRequest == null) {
            villageRequest = new VillageRequest();
        }
        LogUtils.i("村级列表", "url: " + addnew_url);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token_data);
        HttpUtils.requestGet(_mActivity, addnew_url, map, villageRequest);
    }


    private class ContyRequest extends Request {
        @Override
        public void success(String jsonStr) {
            super.success(jsonStr);
            LogUtils.i("乡级列表", "success: " + jsonStr);
            Type type = new TypeToken<ArrayList<CountyEntity>>() {
            }.getType();
            contylist = JsonUtil.json2Collection(jsonStr, type);
        }

        @Override
        public void error(String error) {
            super.error(error);
            LogUtils.i("乡级列表", "error: " + error);
        }
    }

    private class TwonRequest extends Request {
        @Override
        public void success(String jsonStr) {
            super.success(jsonStr);

            LogUtils.i("县级列表", "success: " + jsonStr);

            Type type = new TypeToken<ArrayList<CountyEntity>>() {
            }.getType();
            twonlist = JsonUtil.json2Collection(jsonStr, type);
            cancelLoadingDialog();
        }

        @Override
        public void error(String error) {
            super.error(error);
            LogUtils.i("县级列表", "error: " + error);
            cancelLoadingDialog();
        }
    }

    private class VillageRequest extends Request {
        @Override
        public void success(String jsonStr) {
            super.success(jsonStr);
            LogUtils.i("村级列表", "success: " + jsonStr);
            Type type = new TypeToken<ArrayList<CountyEntity>>() {
            }.getType();
            villagelist = JsonUtil.json2Collection(jsonStr, type);
            cancelLoadingDialog();
        }

        @Override
        public void error(String error) {
            super.error(error);
            LogUtils.i("村级列表", "error: " + error);
            cancelLoadingDialog();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();
    }
}
